library(ltm)
library(dplyr)
library(foreign)
library(readstata13)
library(haven)

load("timeexperiment_pre_manipulation.rdata")

#experimental condition
timeexperiment$Condition[timeexperiment$Q347_Click.Count != ""] <- "Black Organization"
timeexperiment$Condition[timeexperiment$Q355_Click.Count != ""] <- "Race Neutral Organization"

##only keep those who answered support##
timeexperiment$blacksupport <- as.numeric(timeexperiment$Q415_1)
timeexperiment$whitesupport <- as.numeric(timeexperiment$Q514_1)

timeexperiment <- subset(timeexperiment, blacksupport >= 0 | whitesupport >= 0)

table(timeexperiment$Condition)

#code dependent variable####
#number of questions answered about Black organization
timeexperiment$QuestionsBlack <- 0
timeexperiment$Q353_1 <- as.numeric(timeexperiment$Q353_1)
timeexperiment$Q374 <- as.numeric(timeexperiment$Q374)
timeexperiment$Q372 <- as.numeric(timeexperiment$Q372)
timeexperiment$Q376 <- as.numeric(timeexperiment$Q376)
timeexperiment$Q377 <- as.numeric(timeexperiment$Q377)
timeexperiment$Q378 <- as.numeric(timeexperiment$Q378)
timeexperiment$Q379 <- as.numeric(timeexperiment$Q379)

timeexperiment$QuestionsBlack1 <- 0
timeexperiment$QuestionsBlack1[timeexperiment$Q353_1 >= 0] <- 1

timeexperiment$QuestionsBlack2 <- 0
timeexperiment$QuestionsBlack2[timeexperiment$Q367 != ""] <- 1

timeexperiment$QuestionsBlack3 <- 0
timeexperiment$QuestionsBlack3[timeexperiment$Q369.1 != ""] <- 1

timeexperiment$QuestionsBlack4 <- 0
timeexperiment$QuestionsBlack4[timeexperiment$Q374 >= 0] <- 1

timeexperiment$QuestionsBlack5 <- 0
timeexperiment$QuestionsBlack5[timeexperiment$Q370 != ""] <- 1

timeexperiment$QuestionsBlack6 <- 0
timeexperiment$QuestionsBlack6[timeexperiment$Q372 >= 0] <- 1

timeexperiment$QuestionsBlack7<- 0
timeexperiment$QuestionsBlack7[timeexperiment$Q373 != ""] <- 1

timeexperiment$QuestionsBlack8<- 0
timeexperiment$QuestionsBlack8[timeexperiment$Q376 >= 0] <- 1

timeexperiment$QuestionsBlack9<- 0
timeexperiment$QuestionsBlack9[timeexperiment$Q375 != ""] <- 1

timeexperiment$QuestionsBlack10<- 0
timeexperiment$QuestionsBlack10[timeexperiment$Q377 >= 0] <- 1

timeexperiment$QuestionsBlack11<- 0
timeexperiment$QuestionsBlack11[timeexperiment$Q378 >= 0] <- 1

timeexperiment$QuestionsBlack12<- 0
timeexperiment$QuestionsBlack12[timeexperiment$Q379 >= 0] <- 1

timeexperiment$QuestionsBlack <- timeexperiment$QuestionsBlack1 + 
  timeexperiment$QuestionsBlack2 + timeexperiment$QuestionsBlack3 + timeexperiment$QuestionsBlack4+ 
  timeexperiment$QuestionsBlack5+timeexperiment$QuestionsBlack6+timeexperiment$QuestionsBlack7+
  timeexperiment$QuestionsBlack8+timeexperiment$QuestionsBlack9+timeexperiment$QuestionsBlack10

##white questions
#number of questions answered about the race neutral organization
timeexperiment$Q417_1 <- as.numeric(timeexperiment$Q417_1)
timeexperiment$Q398 <- as.numeric(timeexperiment$Q398)
timeexperiment$Q372 <- as.numeric(timeexperiment$Q372)
timeexperiment$Q392 <- as.numeric(timeexperiment$Q392)
timeexperiment$Q394 <- as.numeric(timeexperiment$Q394)
timeexperiment$Q395 <- as.numeric(timeexperiment$Q395)
timeexperiment$Q396 <- as.numeric(timeexperiment$Q396)

timeexperiment$QuestionsWhite1 <- 0
timeexperiment$QuestionsWhite1[timeexperiment$Q417_1 >= 0] <- 1

timeexperiment$QuestionsWhite2 <- 0
timeexperiment$QuestionsWhite2[timeexperiment$Q390 != ""] <- 1

timeexperiment$QuestionsWhite3 <- 0
timeexperiment$QuestionsWhite3[timeexperiment$Q398 >= 0] <- 1

timeexperiment$QuestionsWhite4 <- 0
timeexperiment$QuestionsWhite4[timeexperiment$Q391 != ""] <- 1

timeexperiment$QuestionsWhite5 <- 0
timeexperiment$QuestionsWhite5[timeexperiment$Q392 >= 0] <- 1

timeexperiment$QuestionsWhite6 <- 0
timeexperiment$QuestionsWhite6[timeexperiment$Q399 != ""] <- 1

timeexperiment$QuestionsWhite7<- 0
timeexperiment$QuestionsWhite7[timeexperiment$Q394 >= 0] <- 1

timeexperiment$QuestionsWhite8<- 0
timeexperiment$QuestionsWhite8[timeexperiment$Q393 != ""] <- 1

timeexperiment$QuestionsWhite9<- 0
timeexperiment$QuestionsWhite9[timeexperiment$Q395 >= 0] <- 1

timeexperiment$QuestionsWhite10<- 0
timeexperiment$QuestionsWhite10[timeexperiment$Q396 >= 0] <- 1


timeexperiment$QuestionsWhite <- timeexperiment$QuestionsWhite1 + 
  timeexperiment$QuestionsWhite2 + timeexperiment$QuestionsWhite3 + timeexperiment$QuestionsWhite4+ 
  timeexperiment$QuestionsWhite5+timeexperiment$QuestionsWhite6+timeexperiment$QuestionsWhite7+
  timeexperiment$QuestionsWhite8+timeexperiment$QuestionsWhite9+timeexperiment$QuestionsWhite10


#omnibus DV#
timeexperiment$QuestionsWhite[timeexperiment$condition == "Black Organization"] <- NA
timeexperiment$QuestionsBlack[timeexperiment$condition == "White Organization"] <- NA

timeexperiment$Questions <- (rowMeans(timeexperiment[c("QuestionsBlack", "QuestionsWhite")], na.rm=TRUE))

#group consciousness measures####
#racial group importance#
timeexperiment$ridimp<- c()

timeexperiment$Q207 <- as.numeric(timeexperiment$Q207)
timeexperiment$Q215 <- as.numeric(timeexperiment$Q215)

timeexperiment$ridimp<-ifelse(timeexperiment$Q207 >= 0, timeexperiment$Q207, 
                                    ifelse(timeexperiment$Q215 >=0, timeexperiment$Q215, NA))
timeexperiment$ridimp<-(timeexperiment$ridimp - 6) / -4 

##How important is being Black or African-American to your self-image##
timeexperiment$selfimage <- c()

timeexperiment$Q209 <- as.numeric(timeexperiment$Q209)
timeexperiment$Q217 <- as.numeric(timeexperiment$Q217)

timeexperiment$selfimage<-ifelse(timeexperiment$Q209 >= 0, timeexperiment$Q209, 
                                       ifelse(timeexperiment$Q217 >=0, timeexperiment$Q217, NA))
timeexperiment$selfimage<-(timeexperiment$selfimage - 6) / -4 

#How much is being Black an important part of how you see yourself?#
timeexperiment$impself <- c()

timeexperiment$identity1 <- as.numeric(timeexperiment$identity1)

timeexperiment$impself<-timeexperiment$identity1
timeexperiment$impself<-(timeexperiment$impself - 5) / -4 

###Does R think about being Black lot/fairly often/once in a while/or hardly ever??##
##cant find this just gonna use common fate measure##

timeexperiment$black.fate <- as.numeric(timeexperiment$black.fate)

timeexperiment$black.fate[timeexperiment$black.fate == 4] <- 0
timeexperiment$black.fate[timeexperiment$black.fate == 3] <- .33
timeexperiment$black.fate[timeexperiment$black.fate == 2] <- .66

##What does R say is more important to R -- being 
#Black/being American, or are both eQually important?

timeexperiment$autonomy <- as.numeric(timeexperiment$autonomy)

timeexperiment$autonomy[timeexperiment$autonomy == 2] <- .5
timeexperiment$autonomy[timeexperiment$autonomy == 3] <- 0

##feeling therm black americans##
timeexperiment$ftblack_1 <- as.numeric(timeexperiment$ftblack_1)

timeexperiment$ftblack <- timeexperiment$ftblack_1 / 100

##how close do you feel to blacks##
timeexperiment$closeness_2 <- as.numeric(timeexperiment$closeness_2)

timeexperiment$close<-timeexperiment$closeness_2
timeexperiment$close[timeexperiment$close == 1] <-  0
timeexperiment$close[timeexperiment$close == 2] <-  0.33
timeexperiment$close[timeexperiment$close == 3] <-  0.66
timeexperiment$close[timeexperiment$close == 4] <-  1

##how much discrimination against Black Americans##
timeexperiment$discrim_1 <- as.numeric(timeexperiment$discrim_1)

timeexperiment$discrim<-timeexperiment$discrim_1
timeexperiment$discrim[timeexperiment$discrim == 1] <-  0
timeexperiment$discrim[timeexperiment$discrim == 2] <-  0.33
timeexperiment$discrim[timeexperiment$discrim == 3] <-  0.66
timeexperiment$discrim[timeexperiment$discrim == 4] <-  1

##rr##
timeexperiment$rr4 <- as.numeric(timeexperiment$rr4)

timeexperiment$rrdiscrim<-timeexperiment$rr4
timeexperiment$rrdiscrim[timeexperiment$rrdiscrim == 5] <-  0
timeexperiment$rrdiscrim[timeexperiment$rrdiscrim == 4] <-  0.75
timeexperiment$rrdiscrim[timeexperiment$rrdiscrim == 3] <-  0.5
timeexperiment$rrdiscrim[timeexperiment$rrdiscrim == 2] <-  0.25

###If Black Americans don't do well in life, it is because of Race#
timeexperiment$efficacy <- as.numeric(timeexperiment$efficacy)
timeexperiment$efficacymulti <- as.numeric(timeexperiment$efficacymulti)

timeexperiment$keptback <- c()
timeexperiment$keptback<-ifelse(timeexperiment$efficacy >= 0, timeexperiment$efficacy, 
                                      ifelse(timeexperiment$efficacymulti >=0, timeexperiment$efficiacymulti, NA))
timeexperiment$keptback[timeexperiment$keptback == 2] <-  0

###power black americans hold in american politics##
timeexperiment$influence <- as.numeric(timeexperiment$influence)
timeexperiment$rimulti <- as.numeric(timeexperiment$rimulti)

timeexperiment$power <- c()
timeexperiment$power<-ifelse(timeexperiment$influence >= 0, timeexperiment$influence, 
                                   ifelse(timeexperiment$rimulti >=0, timeexperiment$rimulti, NA))
timeexperiment$power[timeexperiment$power == 1] <-  0
timeexperiment$power[timeexperiment$power == 2] <-  0.5
timeexperiment$power[timeexperiment$power == 3] <-  1

###It is important for people to work together to 
##improve the position of their racial or ethnic group#
timeexperiment$collective <- as.numeric(timeexperiment$collective)

timeexperiment$collective[timeexperiment$collective == 1] <-0
timeexperiment$collective[timeexperiment$collective == 2] <-0.25
timeexperiment$collective[timeexperiment$collective == 3] <-0.5
timeexperiment$collective[timeexperiment$collective == 4] <-0.75
timeexperiment$collective[timeexperiment$collective == 5] <-1

#if enough black americans vote they can make a difference#
timeexperiment$efficacy1 <- as.numeric(timeexperiment$efficacy1)
timeexperiment$efficacymulti <- as.numeric(timeexperiment$efficacymulti)

timeexperiment$votediff <- c()
timeexperiment$votediff<-ifelse(timeexperiment$efficacy1 >= 0, timeexperiment$efficacy1, 
                                      ifelse(timeexperiment$efficacymulti >=0, timeexperiment$efficacymulti, NA))
timeexperiment$votediff[timeexperiment$votediff == 1] <-  0
timeexperiment$votediff[timeexperiment$votediff == 2] <-  0.25
timeexperiment$votediff[timeexperiment$votediff == 3] <-  0.5
timeexperiment$votediff[timeexperiment$votediff == 4] <-  0.75
timeexperiment$votediff[timeexperiment$votediff == 5] <-  1

###To have power and improve their position in the United
#States Black people should be more active in black organizations
timeexperiment$collectiveaction_1 <- as.numeric(timeexperiment$collectiveaction_1)
timeexperiment$collectivemultirace_1 <- as.numeric(timeexperiment$collective.multirace_1)

timeexperiment$blackorg <- c()
timeexperiment$blackorg<-ifelse(timeexperiment$collectiveaction_1 >= 0, timeexperiment$collectiveaction_1, 
                                      ifelse(timeexperiment$collectivemultirace_1 >=0, timeexperiment$collectivemultirace_1, NA))
timeexperiment$blackorg[timeexperiment$blackorg == 1] <-  0
timeexperiment$blackorg[timeexperiment$blackorg == 2] <-  0.25
timeexperiment$blackorg[timeexperiment$blackorg == 3] <-  0.5
timeexperiment$blackorg[timeexperiment$blackorg == 4] <-  0.75
timeexperiment$blackorg[timeexperiment$blackorg == 5] <-  1

###Do you think that the movement for 
#Black rights has affected you personally?
timeexperiment$black.rights <- as.numeric(timeexperiment$black.rights)

timeexperiment$black.rights[timeexperiment$black.rights == 2] <- 0

##To have power and improve their position in the United States 
#each Black person should work hard to 
#improve his or her own personal situation
timeexperiment$collectiveaction_2 <- as.numeric(timeexperiment$collectiveaction_2)
timeexperiment$collectivemultirace_2 <- as.numeric(timeexperiment$collective.multirace_2)

timeexperiment$workhardindv <- c()
timeexperiment$workhardindv<-ifelse(timeexperiment$collectiveaction_2 >= 0, timeexperiment$collectiveaction_2, 
                                          ifelse(timeexperiment$collectivemultirace_2 >=0, timeexperiment$collectivemultirace_2, NA))
timeexperiment$workhardindv[timeexperiment$workhardindv == 2] <-  0.75
timeexperiment$workhardindv[timeexperiment$workhardindv == 3] <-  0.5
timeexperiment$workhardindv[timeexperiment$workhardindv == 4] <-  0.25
timeexperiment$workhardindv[timeexperiment$workhardindv == 5] <-  0

#linkedfate#
timeexperiment$LF1 <- as.numeric(timeexperiment$LF1)
timeexperiment$LF2 <- as.numeric(timeexperiment$LF2)
timeexperiment$LFmulti1 <- as.numeric(timeexperiment$LFmulti1)
timeexperiment$LFmulti2 <- as.numeric(timeexperiment$LFmulti2)

timeexperiment$linkfate <- c()
timeexperiment$linkfate[timeexperiment$LF1 == 1 & timeexperiment$LF2 == 3] <- 1
timeexperiment$linkfate[timeexperiment$LF1 == 1 & timeexperiment$LF2 == 2] <- .66
timeexperiment$linkfate[timeexperiment$LF1 == 1 & timeexperiment$LF2 == 1] <- .33
timeexperiment$linkfate[timeexperiment$LF1 == 2] <- 0

timeexperiment$linkfate[timeexperiment$LFmulti1 == 1 & timeexperiment$LFmulti2 == 3] <- 1
timeexperiment$linkfate[timeexperiment$LFmulti1 == 1 & timeexperiment$LFmulti2 == 2] <- .66
timeexperiment$linkfate[timeexperiment$LFmulti1 == 1 & timeexperiment$LFmulti2 == 1] <- .33

###rgc
timeexperiment$rgcmean <- (rowMeans(timeexperiment[c("ridimp", "selfimage", "impself", 
                                                                 "black.fate", "autonomy", "ftblack",
                                                                 "close","discrim","rrdiscrim",
                                                                 "keptback", "power", "collective",
                                                                 "votediff", "blackorg", "black.rights", 
                                                                 "workhardindv", "linkfate")], na.rm=TRUE))
summary(timeexperiment$rgcmean)
###cronbach's alpha###

myvars <- c("ridimp", "selfimage", "impself", 
            "black.fate", "autonomy", "ftblack",
            "close","discrim","rrdiscrim",
            "keptback", "power", "collective",
            "votediff", "blackorg", "black.rights", 
            "workhardindv", "linkfate")

ndexp <- timeexperiment[myvars]

cronbach.alpha(ndexp, na.rm = TRUE)

#demographic controls and controls for DV####
#pre-intention take part in protest
table(timeexperiment$Q298)
timeexperiment$protest <- timeexperiment$Q298
timeexperiment$protest <- as.numeric(timeexperiment$protest)
timeexperiment$protest <- timeexperiment$protest/4

#pre-intention take part in protest for race
table(timeexperiment$Q178)
timeexperiment$protest4race <- timeexperiment$Q178
timeexperiment$protest4race <- as.numeric(timeexperiment$protest4race)
timeexperiment$protest4race <- timeexperiment$protest4race/4

###descriptive statistics of experiment#
timeexperiment$income <- as.numeric(timeexperiment$inc)
median(timeexperiment$income, na.rm = TRUE)
timeexperiment$education<-as.numeric(timeexperiment$edu)
median(timeexperiment$education, na.rm = TRUE) 

timeexperiment$female <- 0
timeexperiment$female[timeexperiment$Gender == "2"] <- 1
mean(timeexperiment$female)

timeexperiment$age<-as.numeric(timeexperiment$y_born)
median(timeexperiment$age, na.rm = TRUE)

timeexperiment$democrat[timeexperiment$PID == "2"] <- 0
timeexperiment$democrat[timeexperiment$PID == "4"] <- 0
timeexperiment$democrat[timeexperiment$PID == "3" & timeexperiment$Pid.Ind == "3"] <- 0
timeexperiment$democrat[timeexperiment$PID == "3" & timeexperiment$Pid.Ind == "2"] <- 0
timeexperiment$democrat[timeexperiment$PID == "3" & timeexperiment$Pid.Ind == "1"] <- 1
timeexperiment$democrat[timeexperiment$PID == "1"] <- 1

mean(timeexperiment$democrat, na.rm = TRUE)

timeexperiment$ideology <-as.numeric(timeexperiment$Ideo)



####keep only the variables i need####
timeexperiment <-subset(timeexperiment, race == "2" | multirace == "2")

timedv_final2 <- timeexperiment %>%
  dplyr::select(ridimp, selfimage, impself, black.fate, autonomy, ftblack,
                close, discrim, rrdiscrim, keptback, power, collective, votediff, blackorg,
                black.rights, workhardindv, linkfate, rgcmean,
                Questions, blacksupport, whitesupport, Condition, att_check, QuestionsBlack,
                QuestionsWhite, race, multirace, black, blackorg, income, education, female,
                age, democrat, ideology, protest, protest4race)

write.csv(timedv_final, "timedv_final.csv")
